home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2074 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question about system call
  5. Date: Thu, 18 Jan 96 22:04:48 GMT
  6. Organization: none
  7. Message-ID: <822002688snz@genesis.demon.co.uk>
  8. References: <4dekv8$9ja@spider.hik.se> <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
  15.            Alun.Champion@bridge.bst.bls.com "Alun Champion" writes:
  16.  
  17. >: An example:
  18. >
  19. >:   write(0, (the integer value), sizeof(int));
  20.  
  21. >1) Try:
  22. >
  23. >  write(0, &int_value, sizeof(int));
  24.  
  25. That assumes (the integer value) is an lvalue, not a general expression (or
  26. even a constant).
  27.  
  28. >2) Try:
  29. >
  30. >  char buf[10];
  31. >  sprintf(buf, "%d", int_value);
  32. >
  33. >  write(0, buf, strlen(buf)-1);
  34.  
  35. strlen returns the number of characters in the string before the '\0' so
  36. you don't want the -1. sprintf returns this value anyway so there's no
  37. need to call strlen.
  38.  
  39. >A couple of points, this is not portable, why not use the stdio library,
  40. >this would then become
  41. >
  42. >1)
  43. >  printf("%*s", sizeof(int), (char*)&int_value);
  44.  
  45. No, %s outputs a string i.e. stops if it hits a '\0' which it may well
  46. do within the internal representation of an integer. Also * reads an
  47. int argument so you need (int)sizeof(int). A close standard equivalent is:
  48.  
  49.    fwrite(&int_val, sizeof(int), 1, stdout);
  50.  
  51. -- 
  52. -----------------------------------------
  53. Lawrence Kirby | fred@genesis.demon.co.uk
  54. Wilts, England | 70734.126@compuserve.com
  55. -----------------------------------------
  56.